package cn.broadsense.thrid.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * 接口返回状态码枚举
 *
 * @author pengxin
 * @date 2022-08-08 16:47
 */
@Getter
@AllArgsConstructor
public enum ResultStatusEnum {

    OK(200, "请求成功"),
    CREATED(201, "已创建"),
    UNAUTHORIZED(401, "未授权"),
    ;

    /**
     * 状态编码
     */
    public int code;

    /**
     * 描述
     */
    public String msg;


    /**
     * 根据编号获取描述信息
     *
     * @param code
     * @return
     */
    public static String getMsgByCode(int code) {
        Optional<ResultStatusEnum> first = Stream.of(ResultStatusEnum.values()).filter(item -> item.code == code).findFirst();
        if (first.isPresent()) {
            return first.get().getMsg();
        }
        return null;
    }

}
